package com.lfg.leetcode.leetcode0;


public class No52totalNQueens {

    public static void main(String[] args) {
        Integer ret = totalNQueens(4);
        System.out.println(ret);
    }

    public static int totalNQueens(int n) {
        int[] y = new int[n];
        int ret = solveNQueens(n, y, 0);
        return ret;
    }

    public static int solveNQueens(int n, int[] y, int rowNo) {
        int ret = 0;
        if (rowNo == n) {
            return 1;
        }
        for (int j = 0; j < n; j++) {
            boolean flag = true;
            for (int i = 0; i < rowNo; i++) {
                Integer ey = y[i];
                if (ey == j) {
                    flag = false;
                    break;
                }
                if (i - rowNo == j - ey || i - ey == rowNo - j) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                y[rowNo] = j;
                ret += solveNQueens(n, y, rowNo + 1);
            }
        }
        return ret;
    }
}

